<?php
	/*
	file ini digunakan untuk mengontrol login apakah user telah sesuai prosedur masuk aplikasi
	*/
class c_berita extends CI_Controller {

	function __construct(){
		parent::__construct();
		session_start();
		$this->load->model('m_berita');
		if(!isset($_SESSION[SESSION_USERID])){
			redirect(base_url());
		}

	}//end Login

	function get_action(){
		$task = isset($_POST['task']) ? @$_POST['task'] : $_GET['task'];
		switch($task){
			case "list":
				$this->berita_list();
			break;
			case "create":
				$this->berita_create();
			break;
			case "update":
				$this->berita_update();
			break;
			case "delete":
				$this->berita_delete();
			break;
			case "search":
				$this->berita_search();
			break;
			default:
				echo "{ failure : true }";
			break;
		}
	}

	function index(){
		$data["title"]="BERITA";
		$data["content"]=$this->load->view('admin/v_berita_list',$data,TRUE);
		$this->load->view('admin/v_template',$data);
	}

	function berita_load(){
		$this->load->view('admin/v_berita_list');
	}

	function terbaru(){
		$data["title"]="BERITA TERBARU";
		$data["menus"]=$this->m_public_function->get_menu_home();
		$data["content"]=$this->m_berita->berita_terbaru();
		$this->load->view('v_berita_terbaru',$data);
	}

	function view(){
		$data["title"]="BERITA TERBARU";
		$permalink=$_GET['p'];
		$data["content"]=$this->m_berita->get_view($permalink);
		$this->load->view('v_berita_view',$data);
	}

	function listview(){

		$query = isset($_POST['query']) ? $_POST['query'] : "";

		$config['base_url'] = '/c_berita/listview';
		$config['total_rows'] = $this->m_berita->berita_get_count_rows($query);
		$config['per_page'] = '20';
		$config['uri_segment'] = 3;

		$this->pagination->initialize($config);


		$per_page = (integer) (isset($_GET['per_page']) ? $_GET['per_page'] : 0);
		$start = $per_page+1;
		$end = $config['per_page'];


		$data["title"]="BERITA";
		$data["menus"]=$this->m_public_function->get_menu_home();
		$data["content"]=$this->m_berita->berita_listview($query, $start, $end);
		$data["page"]=$this->pagination->create_links();

		$this->load->view('v_berita_listview',$data);
	}

	function berita_add(){
		//$data["title"]="BERITA";
		//$data["content"]=$this->load->view('admin/v_berita_post',$data,TRUE);
		//$this->load->view('admin/v_template',$data);
		$this->load->view('admin/v_berita_post');
	}

	function berita_addedit(){
		$data = array();
		$id = $this->uri->segment(3);
		if($id != false){
			$data['dataDetail'] = $this->m_berita->getDetail($id);
		}
		$this->load->view('admin/v_berita_post', $data);
	}

	function berita_list(){
		$query = $this->input->get('query');
		$tglawal = $this->input->get('tglawal');
		$tglakhir = $this->input->get('tglakhir');
		$pagenum = $_GET['pagenum'];
		$pagesize = $_GET['pagesize'];
		$start = $pagenum * $pagesize;
		$end = $start + $pagesize;

		$result=$this->m_berita->berita_list($query, $start, $end, $tglawal, $tglakhir);
		echo $result;

	}

	function berita_save(){
		$id = $this->input->post('id');
		$kategori = $this->input->post('kategori');
		$kategori = is_numeric($kategori) ? $kategori : 0;
		$judul = $this->input->post('judul');
		$tanggal = $this->input->post('tanggal');
		$tag = $this->input->post('tag');
		$isi = $this->input->post('isi');
		$headline = $this->input->post('headline');
		$publish = $this->input->post('publish');
		$gambar = $this->input->post('gambar');
		$task = $this->input->post('task');
		$created_by = $_SESSION[SESSION_USER];
		$data = array(
			'judul'=>$judul,
			'created_date'=>$tanggal,
			'created_by'=>$created_by,
			'tag'=>$tag,
			'isi_berita'=>$isi,
			'headline'=>$headline,
			'publish'=>$publish,
			'image_berita'=>$gambar
		);
		if($kategori != 0){
			$data["kategori_id"]=$kategori;
		}
		$params = array(
			'id'=>$id,
			'data'=>$data,
			'task'=>$task
		);
		$result = $this->m_berita->berita_save($params);
		echo $result;
	}

	function berita_delete(){
		$id = $this->input->post('id');
		$id = is_numeric($id) ? $id : $id;
		$result = $this->m_berita->berita_delete($id);
		echo $result;
	}

	function berita_search(){

	}

	function berita_update(){

	}

	function berita_create(){

	}
	
	function berita_switchpublish(){
		$id = $this->input->post('id');
		$id = is_numeric($id) ? $id : 0;
		$stats = $this->input->post('stats');
		$result = $this->m_berita->berita_switchpublish($id, $stats);
		echo $result;
	}

}
?>
